package com.example.wms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import com.example.classes.DataBase;
import com.example.classes.Member;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Activity for managing viewing the list of all Members in the database.
 * @author      Alexander Huynh <a.huynh @ gatech.edu>
 * @version     1.0
 * @since       2013-03-09
 */

// -- Alex: example for displaying images: http://www.mkyong.com/android/android-listview-example/

public class MemberListViewActivity extends Activity{

    private ListView listView;
    private DataBase d;
    private EditText editText;
    private int txtLength = 0;
    private String[] listview_array;
    private ArrayAdapter<String> listAdapter;
    private final ArrayList<String> array_sort= new ArrayList<String>();
    private Intent intent;
    private String username;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        d = new DataBase(this);
        setContentView(R.layout.memberlistview);
        listview_array = populateList();
        listView = (ListView) findViewById(R.id.ListView01);
        editText = (EditText) findViewById(R.id.EditText01);

        Intent parentIntent = getIntent();
        username = parentIntent.getStringExtra("username");

        intent = new Intent(this, ModifyHandler.class);

        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listview_array)); //listview_array

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parentView, View childView, int position, long id)
            {
                intent.putExtra("username", username);
                intent.putExtra("selectedname", listview_array[position]);
                Log.v("WMS-DEBUG", "MEMBERVIEW: position = " + position + "    id = " + id);
                Log.v("WMS-DEBUG", "MEMBERVIEW: array value at position: " + listview_array[position]);
                startActivity(intent);
                finish();
            }
        });

        editText.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s)
            {
                txtLength = editText.getText().length();
                array_sort.clear();
                for (String aListview_array : listview_array)
                {
                    if (txtLength <= aListview_array.length())
                    {
                        if (aListview_array.contains(editText.getText().toString()))
                        {
                            array_sort.add(aListview_array);
                        }
                    }
                }
                listAdapter = new ArrayAdapter<String>(MemberListViewActivity.this, android.R.layout.simple_expandable_list_item_1, array_sort);
                listView.setAdapter(listAdapter);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                txtLength = editText.getText().length();
                array_sort.clear();
                for (String aListview_array : listview_array)
                {
                    if (txtLength <= aListview_array.length())
                    {
                        if (aListview_array.contains(editText.getText().toString()))
                        {
                            array_sort.add(aListview_array);
                        }
                    }
                }
                listAdapter = new ArrayAdapter<String>(MemberListViewActivity.this, android.R.layout.simple_expandable_list_item_1, array_sort);
                listView.setAdapter(listAdapter);
            }
        });
    }

    /**
     * Populates the ListView with all of the Members in the database.
     */
    private String[] populateList()
    {
        d.open();
        Collection<Member> members = d.getAllUsers();
        d.close();
        String[] finalMembers = new String[members.size()];
        int i = 0;
        for (Member m: members)
        {
            finalMembers[i++] = m.getName();
        }
        return finalMembers;
    }
}